package com.extremelylargepackage;

import java.util.Collection;
import java.util.LinkedList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;

import com.extremelylargepackage.entity.Button;
import com.extremelylargepackage.entity.ScrollingBackground;

public class MenuEngine extends Engine
{
	private ScrollingBackground _background;
	private Collection<Button> _buttons;
	
	public MenuEngine(ThreadCallBack thread)
	{
		super(thread);
	}
	
	@Override
	protected void onInitialize() 
	{
		_buttons = new LinkedList<Button>();
		
//		Button tapToBegin = new Button(GameResources.SCREEN_WIDTH / 2f, GameResources.SCREEN_HEIGHT * .85f);
//		tapToBegin.setText("Tap to play");
//		_buttons.add(tapToBegin);
		
		float center = GameResources.SCREEN_WIDTH / 2f;
		
		Button title = new Button(center, GameResources.SCREEN_HEIGHT * .15f);
		title.setText("schmup");
		title.setTextSize(GameResources.getTitleFontSize());

		Button campaign = new Button(center, GameResources.SCREEN_HEIGHT * .45f);
		campaign.setText("Campaign");
		campaign.setButtonPressListener(new CampaignHandler());
		campaign.setHighlight(Color.YELLOW);
	
		Button custom = new Button(center, campaign.getBottom() + (GameResources.SCREEN_HEIGHT * .05f));
		custom.setText("Custom Level");
		custom.setButtonPressListener(new CustomHandler());
		custom.setHighlight(Color.YELLOW);
		
		Button editor = new Button(center, custom.getBottom() + (GameResources.SCREEN_HEIGHT * .05f));
		editor.setText("Level Editor");
		editor.setButtonPressListener(new LevelEditHandler());
		editor.setHighlight(Color.YELLOW);
		
		Button settings = new Button(center, editor.getBottom() + (GameResources.SCREEN_HEIGHT * .05f));
		settings.setText("Settings");
		settings.setButtonPressListener(new SettingsHandler());
		settings.setHighlight(Color.YELLOW);
		
		_buttons.add(title);
		_buttons.add(campaign);
		_buttons.add(custom);
		_buttons.add(editor);
		_buttons.add(settings);
		
		_background = new ScrollingBackground(GameResources.getResource("title_nc"));
	}
	
	private class CampaignHandler implements Button.ButtonPressListener
	{
		@Override
		public void onClick() 
		{
			_thread.startLevelSelect(false);
		}
	}
	
	private class CustomHandler implements Button.ButtonPressListener
	{
		@Override
		public void onClick()
		{
				_thread.startLevelSelect(true);
		}
	}
	
	private class LevelEditHandler implements Button.ButtonPressListener
	{
		@Override
		public void onClick() 
		{
			_thread.startGarage();
		}
	}
	
	private class SettingsHandler implements Button.ButtonPressListener
	{
		@Override
		public void onClick() 
		{
			
		}	
	}

	@Override
	public void draw(Canvas c) 
	{
		_background.draw(c);
		for(Button button : _buttons)
		{
			button.draw(c);
		}
	}

	@Override
	public void onTouch(MotionEvent event) 
	{
		for(Button b : _buttons)
		{
			if(b.containsPoint(event.getX(), event.getY()))
			{
				b.hightlight();
				if(event.getAction() == MotionEvent.ACTION_UP)
				{
					b.press();
					b.unHightlight();
				}
			}
			else
			{
				b.unHightlight();
			}
		}
	}

	@Override
	protected void updateState(long interval) 
	{
		// Do nothing	
	}
}
